package cn.guestbook.utils;

import java.io.Serializable;

import org.junit.Test;

/**
 * @author Wangzi
 * @email yhjhoo@gmail.com
 *
 */
public class Page implements Serializable{


	private static final long serialVersionUID = 1L;
	
	private int pageSize = 10;
	private int recordSum = 0;
	private int pageNoSize = 7;//how many page no can be displayed
	private int currentPageNo = 1;	
	private boolean hasNextPage = false;
	private boolean hasPrePage = false;
	private int maxPageNo = 0;
	private int[] pageNoArray;
	private int firstRecord;//current page's first record	
	
	public Page() {
		//recordSum = 100; //initianl records
		this.maxPageNo = getMaxPageNo();
	}
	
	@Test
	public void test(){
		Page page = new Page();
		page.setCurrentPageNo(0);
		page.getFirstRecord();
		page.getPageNoArray();
		
		page.getHasNextPage();
	}

	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRecordSum() {
		return recordSum;
	}
	public void setRecordSum(int recordSum) {
		this.recordSum = recordSum;
	}
	public int getPageNoSize() {
		return pageNoSize;
	}
	public void setPageNoSize(int pageNoSize) {
		this.pageNoSize = pageNoSize;
	}
	public int getCurrentPageNo() {
		return currentPageNo;
	}
	public void setCurrentPageNo(int currentPageNo) {
		if (currentPageNo < 1)
			currentPageNo = 1;
		if(currentPageNo>getMaxPageNo())
			currentPageNo = getMaxPageNo();
		this.currentPageNo = currentPageNo;
	}

	
	
	public boolean getHasNextPage() {
		if (currentPageNo<this.getMaxPageNo() && this.getMaxPageNo()>1)
			hasNextPage = true;
		else hasNextPage = false;
		return hasNextPage;
	}
	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean getHasPrePage() {
		if(currentPageNo>1)
			hasPrePage = true;
		else hasPrePage = false;
		return hasPrePage;
	}
	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	public int getMaxPageNo() {
		if(recordSum>0){
			int temp = recordSum%pageSize >0?1:0;
			maxPageNo = recordSum/pageSize+ temp;
		}else maxPageNo = 0;
		return maxPageNo;
	}
	public void setMaxPageNo(int maxPageNo) {
		this.maxPageNo = maxPageNo;
	}

	public int[] getPageNoArray() {
		if(currentPageNo<=pageNoSize/2+1){
			pageNoArray = _generatePageNo(1,pageNoSize);
		}
		if(currentPageNo>pageNoSize/2+1 && currentPageNo<=getMaxPageNo()){
			pageNoArray = _generatePageNo(currentPageNo-pageNoSize/2,currentPageNo+pageNoSize/2);
		}
		if(currentPageNo>getMaxPageNo()){
			int start = getMaxPageNo()-pageNoSize+1;
			if(start<0)
				start = 0;
			pageNoArray = _generatePageNo(start,getMaxPageNo());
		}
		return pageNoArray;
	}
	public void setPageNoArray(int[] pageNoArray) {
		this.pageNoArray = pageNoArray;
	}
	
	public int getFirstRecord() {
		firstRecord = (currentPageNo-1)*pageSize ;
		if(firstRecord>recordSum)
			firstRecord = recordSum - pageSize;
		if(firstRecord<0)
			firstRecord = 0;
		return firstRecord;
	}
	public void setFirstRecord(int firstRecord) {
		this.firstRecord = firstRecord;
	}
	
	private int[] _generatePageNo(int start,int end){
//	public void gg(){
//		int start = 0;
//		int end = 10;
		if(start<1){
			start = 1;
		}
		if(end>getMaxPageNo()){
			end = getMaxPageNo();
		}
		int size = end-start+1;
		int[] a = new int[size];
		for(int i = 0;i<size;i++){
			a[i] = start+i;
		}
		return a;
	}

	

	
	
	
}
